package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.TableColumnBiz;
import com.egao.orderB.entity.TableColumn;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2021-09-01 15:19:33
 */
@Repository
public class TableColumnBizImpl extends BaseDao implements TableColumnBiz {

  @Override
  public JsonResult list(PageResult2<T> page, TableColumn tableColumn, User loginUser) throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<TableColumn> rm = new BeanPropertyRowMapper<TableColumn>(TableColumn.class);
    StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.table_name");
    sql.append(",t.order_num,t.title,t.remark");
    sql.append(",t.create_time,t.create_user,t.update_time,t.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_table_column t ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=t.organization_id and sogzn.deleted=0 ");
    sql.append(" where t.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and t.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(tableColumn)) {
//			if(Utils.isNotEmpty(tableColumn.getName())){
//				sql.append(" and instr(t.name,:name)>0 ");
//				map.put("name", tableColumn.getName());
//			}
      if (Utils.isNotEmpty(tableColumn.getSort()) && Utils.isNotEmpty(tableColumn.getOrder())) {
        sql.append(" order by t." + tableColumn.getSort() + " " + tableColumn.getOrder());
      } else {
        sql.append(" order by t.create_time desc");
      }
    }
    return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
  }

  @Override
  public List<TableColumn> list(TableColumn tableColumn, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<TableColumn> rm = new BeanPropertyRowMapper<TableColumn>(TableColumn.class);
    StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.table_name");
    sql.append(",t.order_num,t.title,t.remark");
    sql.append(",t.create_time,t.create_user,t.update_time,t.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_table_column t ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=t.organization_id and sogzn.deleted=0 ");
    sql.append(" where t.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and t.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(tableColumn)) {
//			if(Utils.isNotEmpty(tableColumn.getName())){
//				sql.append(" and instr(t.name,:name)>0 ");
//				map.put("name", tableColumn.getName());
//			}
      if (Utils.isNotEmpty(tableColumn.getSort()) && Utils.isNotEmpty(tableColumn.getOrder())) {
        sql.append(" order by t." + tableColumn.getSort() + " " + tableColumn.getOrder());
      } else {
        sql.append(" order by t.create_time desc");
      }
    }
    getMapParameter(sql, map);
    return jdbcTemplate.query(sql.toString(), map, rm);
  }

  @Override
  public TableColumn getById(TableColumn tableColumn) {
    RowMapper<TableColumn> rm = new BeanPropertyRowMapper<TableColumn>(TableColumn.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.table_name");
    sql.append(",t.order_num,t.title,t.remark");
    sql.append(",t.create_time,t.create_user,t.update_time,t.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_table_column t ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=t.organization_id and sogzn.deleted=0 ");
    sql.append(" where t.deleted=0 and t.id=:id ");
    map.put("id", tableColumn.getId());
    getMapParameter(sql, map);
    return jdbcTemplate.queryForObject(sql.toString(), map, rm);
  }

  @Override
  public boolean save(TableColumn tableColumn, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("insert into chf_table_column (");
    sql.append("organization_id,table_name,order_num,title");
    sql.append(",remark,create_user,create_time) values ( ");
    sql.append(":organization_id,:table_name,:order_num,:title");
    sql.append(",:remark,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("table_name", tableColumn.getTableName());
    map.put("order_num", tableColumn.getOrderNum() == null ? null : tableColumn.getOrderNum() + "");
    map.put("title", tableColumn.getTitle());
    map.put("remark", tableColumn.getRemark());
    map.put("create_user", loginUser.getUserId() + "");
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean update(TableColumn tableColumn, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_table_column set ");
    sql.append("table_name=:table_name,order_num=:order_num,title=:title ");
    sql.append(",remark=:remark,update_time=now() ");
    sql.append(",update_user=:update_user where id=:id ");
    map.put("table_name", tableColumn.getTableName());
    map.put("order_num", tableColumn.getOrderNum() == null ? null : tableColumn.getOrderNum() + "");
    map.put("title", tableColumn.getTitle());
    map.put("remark", tableColumn.getRemark());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", tableColumn.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean remove(TableColumn tableColumn, User loginUser) {
    int count = 0;
    Map<String, String> map = null;
    if (tableColumn.getId().length() > 0) {
      for (String ID : tableColumn.getId().split(",")) {
        map = new HashMap<String, String>();
        if (ID == null || ID.equals("")) {
          break;
        }
        StringBuffer sql = new StringBuffer("update chf_table_column set deleted=1" +
                ",update_user=:update_user, update_time=now() "
                + " where id =:id ");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", ID);
        getMapParameter(sql, map);
        count = jdbcTemplate.update(sql.toString(), map);
      }
    }
    return count > 0;
  }
}
